package com.klun.designMode.proxy.JDKProxy;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;

public class TestFindLove {
    public static void main(String[] args) {
        try {
            Person person = (Person) new MyProxy().getInstance(new Jack());
            person.findLove();
            System.out.println(person.getClass());
            //原理：
            //1.拿到被代理对象的引用，然后获取它的接口
            //2.JDK代理重新生成一个类，同时实现我们给的代理对象所实现的接口
            //3.把被代理对象的引用也拿到了
            //4.重新动态生成了一个class字节码
            //5.然后编译
            //E:\klun\arithmetic\target\classes\com\klun\designMode\proxy\JDKProxy\Person.class
            byte[] data = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{person.getClass()});
            FileOutputStream os = new FileOutputStream("E:\\klun\\arithmetic\\target\\classes\\com\\klun\\designMode\\proxy\\JDKProxy/$Proxy0.class");
            os.write(data);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
